<?php 
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use Sanyc\Aliyun\AliyunEmail;
use app\common\controller\Api as commonApi;
use app\user\traits\controllers\TraitUser;

/**
 * 网站首页相关
 */
class Web extends commonApi {
	//use \app\common\traits\controller\TraitController;
    use \app\user\traits\models\UserInfoCheck;
    use TraitUser;
    //前置操作
    protected $beforeActionList = [
    ];
    
    public function index()
    {
        return $this->getindex();
    }
    /**
     * [getindex 网站首页]
     * @return [type] [description]
     */
    public function getindex()
    {
        //写入url
        if (empty(Session::pull('redirect_url'))) {
            Session::set('redirect_url', Request::instance()->url(true));
        }
        //城市列表
        $city = model('CarCalendar')->getCitys();
		
		if(empty(Session::has('user.id'))){
			$where = [
				'start_date'          => ['EGT', date('Y:m:d',time())],
				'complete_caravan_id' => ['EQ', 0]
			];
			$wishsList = model('Wishs') -> limit(6) -> order('created_at desc') -> where($where) -> select();
		}else{
			$wishsList = $this -> getOtherwishs($date = []);
		}
		
        $newslist = model('supermanager/News')->getNewsInfoLists();
        $this->assign('newslist',$newslist);
        return view('web/index',['city' => $city, 'wishsList' => $wishsList]);
    }

    public function getNewsModel($id)
    {
        $newsinfo = model('supermanager/News')->where('id',$id)->find();
        $this->assign('newsinfo',$newsinfo);
        return view('web/newsmodel');
    }

    /**
     * [getSearchExchange 网站首页交换搜索]
     * @param string $value [description]
     */
    public function getSearchExchange()
    {
        $s = input('get.indexsdate');
        $e = input('get.indexedate');
        if (!empty(input('get.city'))) {
            $a =input('get.city');//halt($a);
            return $this->redirect("/filter/exlistform?sdate={$s}&&edate={$e}&&city={$a}");
        }
        return $this->redirect("/filter/exlistform?sdate={$s}&&edate={$e}&&city=");
    }
    /**
     * [getCheckIntegralEnough 基数判断与弹框//与登录弹框]
     * @return [type] [description]
     */
    public function getCheckIntegralEnough()
    {
        //旅行币小于基数
        // $now_integral = $this->getLoginUserInfo('integral');
        // $camp_id = $this->getLoginUserInfo('camp_id');
        // $travel_money_base = model('Camps')->where('id',$camp_id)->value('travel_money_base');
        // if ($now_integral<$travel_money_base) {
        //     //触发弹出框
        //     return json(0);
        // }
        return 1;
       
    }
    /**
     * [getSearchRent 首页出租搜索表单]
     * @return [type] [description]
     */
    public function getSearchRent()
    {
        $s = input('get.indexsdate');
        $e = input('get.indexedate');
        if (!empty(input('get.city'))) {
            $a =input('get.city');//halt($a);
            return $this->redirect("/filter/exlistform?sdate={$s}&&edate={$e}&&city={$a}");
        }

        return $this->redirect("/filter/exlistform?sdate={$s}&&edate={$e}&&city=");
    }
    /**
     * [getSearchWish 首页接单搜索表单]
     * @return [type] [description]
     */
    public function getSearchWish()
    {
        $start_date = input('get.start_date');
        $end_date = input('get.end_date');
        return $this->redirect(url("/wishs/allwishlist?start_date={$start_date}&end_date={$end_date}",'','','user'));
    }
    /**
     * [FunctionName 热门房车]
     * @param string $value [description]
     */
    public function getHotCaravanList()
    {
       
    }
    /**
     * [getWishList 需求列表]
     * @param string $value [description]
     */
    public function getWishList()
    {
        $list = db('wishs')->limit(5)->select();
        return json($list);
    }
    /**
     * [getJoinCamp 加盟我们界面]
     * @param string $value [description]
     */
    public function getJoinCamp()
    {
       return view('web/joincamp');
    }
    /**
     * [getBuyCaravan 购买房车]
     * @param string $value [description]
     */
    public function getBuyCaravan()
    {
        return view('web/buycaravan');
    }
    /**
     * [getStory1 交换故事1]
     * @param string $value [description]
     */
    public function getStory1()
    {
        return view('web/story01');
    }
    /**
     * [getStory2 交换故事2]
     * @param string $value [description]
     */
    public function getStory2()
    {
        return view('web/story02');
    }
    /**
     * [getStory3 交换故事3]
     * @param string $value [description]
     */
    public function getStory3()
    {
        return view('web/story03');
    }

    public function getCaravan1()
    {
        return view('web/caravan01');
    }

    public function getCaravan2()
    {
        return view('web/caravan02');
    }

    public function getCaravan3()
    {
        return view('web/caravan03');
    }

    /**
    * [getQueryOrder 订单查询页面]
    * @param string $value [description]
    */
    public function getQueryOrder()
    {
       return view('web/orderinquire');
    }
    /**
     * [postOrderNumberWay 订单号查询方式]
     * @param string $value [description]
     */
    public function postOrderNumberWay()
    {
        //图片验证码
        $imgCode = input('post.imgCode');
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($imgCode)) {
            return $this->error(lang('图片验证码错误'));
        }
        //其他信息
        $order_number = input('post.number');
        $orderinfos =  model('Orders')->where('number',$order_number)->find();
        return view();//相应界面
    }
    /**
     * [FunctionName 手机号查询]
     * @param string $value [description]
     */
    public function postOrderPhone()
    {
       
    }
    ///////////////////////地区插件////////////////////////
    /**
     * [getCountry 国家]
     * @return [type] [description]
     */
    public function getCountry()
    {
        $country = db('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();
        return json($country);
    }
    /**
     * [getProvince 省份]
     * @return [type] [description]
     */
    public function getProvince()
    {
        $addressProvince = input('addressCountry');
        $province = db('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    /**
     * [getCity 城市]
     * @return [type] [description]
     */
    public function getCity()
    {
       
        $city = input('addressProvince');
        $city = db('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }
    /////////////////////////地区插件结束/////////////////////////
    /**
     * [sendEmail 发送邮箱]
     * @param  [type] $emailAddress [目标邮箱]
     * @param  [type] $theme        [邮件主题]
     * @param  [type] $content      [邮件内容]
     * @return [type]               [description]
     */
    public function sendEmail($emailAddress,$theme,$content)
    {
        $mail = AliyunEmail::init(config('mail'))->send($emailAddress, $theme, $content);
        return $mail;
    }
    public function getCheckEmail()
    {
        $email = input('email');
        $re = $this->cheack(['email'=>$email]);
        if ($re) {
            return 1;
        }else{
            return json(0);
        }
    }
    /**
     * [postJoinForm 测试]
     * @param string $value [description]
     */
    public function postJoinForm()
    {
        $name = input('post.name');
        $phone = input('post.phone');
        $email = input('post.email');
        $re = $this->cheack(['email'=>$email]);
        if (!$re) {
            return $this->error('邮箱格式不正确',$url = null,'',$wait=3);
        }
        $co = input('post.country'); $p = input('post.province'); $c = input('post.city');
        $address = "$co"."$p"."$c";
        $detailed_address = input('post.address');
        $content=<<<data
加盟商信息
营地负责人称谓：$name;
营地负责人手机: $phone;
营地负责人邮箱: $email;
营地地址:$address;
营地详细地址:$detailed_address;
data;
        //$type = input('post.type');
// if (!empty($type)) {
//         $content_buy=<<<da
// 购买人信息
// 购买人称谓：$name;
// 购买人手机: $phone;
// 购买人邮箱: $email;
// 购买人地址:$address;
// 购买人详细地址:$detailed_address;
// da;->格式严格
//         halt($content_buy);
//         $res = $this->sendEmail('752845594@qq.com','我要购买房车',$content_buy);
//         if ($res) {
//             return $this->success('发送成功','/');
//         }else{
//             return $this->error('发送失败',$url = null,'',$wait=3);
//         }
//     }
        $res = $this->sendEmail('joincamp@shsytour.com','我要加盟',$content);
        if ($res) {
            return $this->success('提交成功','/');
        }else{
            return $this->error('提交失败',$url = null,'',$wait=3);
        }
    }

    public function postBuyCar()
    {
        $name = input('post.name');
        $phone = input('post.phone');
        $email = input('post.email');
        $re = $this->cheack(['email'=>$email]);
        if (!$re) {
            return $this->error('邮箱格式不正确',$url = null,'',$wait=3);
        }
        $co = input('post.country'); $p = input('post.province'); $c = input('post.city');
        $address = "$co"."$p"."$c";
        $detailed_address = input('post.address');
        $content_buy=<<<data
购买人信息
购买人称谓：$name;
购买人手机: $phone;
购买人邮箱: $email;
购买人地址:$address;
购买人详细地址:$detailed_address;
data;
        $res = $this->sendEmail('buycaravan@shsytour.com','我要购买房车',$content_buy);
        if ($res) {
            return $this->success('提交成功','/');
        }else{
            return $this->error('提交失败',$url = null,'',$wait=3);
        }
    }
    

}